package com.briup.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.briup.demo.bean.CmsMember;
import com.briup.demo.common.Response;
import com.briup.demo.service.CmsMemberService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "成员用户管理")
@RestController
@RequestMapping("/member")
public class CmsMemberController {

	@Autowired
	private CmsMemberService memberService;
	
	@ApiOperation(value = "显示所有成员用户信息",notes = "从数据库中检索所有用户信息返回")
	@GetMapping("/all")
	public Response<List<CmsMember>> selectAll(){
		List<CmsMember> cm = memberService.getAllMember();
		return Response.ok(cm);
	}
	
	@ApiOperation(value = "保存或更新成员信息")
	@PostMapping("/saveOrUpdate")
	public Response<String> saveOrUpdate(CmsMember member){
		int num = memberService.saveOrUpdate(member);
		if(num==1) {
			return Response.ok("插入信息成功");			
		}else if(num==0){
			return Response.ok("更新信息成功");
		}else {
			return Response.ok("没有这个成员信息");
		}
	}
	
	@ApiOperation(value = "删除成员信息",notes = "按编号来删除用户信息")
	@DeleteMapping("/deleteById")
	public Response<String> deleteById(String id){
		int num = memberService.deleteById(id);
		if(num == 0) {
			return Response.error("该文章不存在");
		}else {
			return Response.ok("删除成功");
		}
	}	
	
	@ApiOperation(value = "按条件检索用户",notes = "按编号来检索用户信息")
	@GetMapping("selectById")
	public Response<CmsMember> selectById(String id){ 
		CmsMember cm = memberService.selectById(id);
		return Response.ok(cm);
	}
	
	@ApiOperation(value = "用户状态")
	@ApiImplicitParams({
		@ApiImplicitParam(name="memberId",value = "用户编号"),
		@ApiImplicitParam(name = "state",value = "状态",
							allowableValues = "正常,异常")
	})
	
	@PutMapping(value = "/updateState")
	public Response<String> updateState(String memberId,
			String state){
		boolean b = memberService.updateState(memberId, state);
		if(b) return Response.ok("操作成功！");
		else return Response.error("要更新的用户不存在！");
	}
	
	
}
